﻿using Groep2TINGHService;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for BedrijvenToevoegen.xaml
    /// </summary>
    public partial class BedrijvenToevoegen : MetroWindow
    {
        //declaratie
        static string Username;
        static string Paswoord;
        static string PaswoordBev;
        static string Vraag;
        static string Antwoord;
        static string Straat;
        static string Postcode;
        static string Plaats;
        static string Land;
        static string Email;
        static string Telefoon;
        static int Medewerkers;
        static string Rol;
        public BedrijvenToevoegen()
        {
            InitializeComponent();

            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Role role = new Role();
            List<Role> lijstRoles = myClient.Get_AllRoles();
            lijstRoles.Add(role);
            foreach (Role r in lijstRoles)
            {
                cmbRoles.Items.Add(r.RoleName);
            }
            Company company = new Company();
            List<Company> lijstCompanies = myClient.GetAllCompanies();
            lijstCompanies.Add(company);
            foreach (Company r in lijstCompanies)
            {
                lstBedrijven.Items.Add(r.Name);
            }
        }

        private void btnToevoegen_Click(object sender, RoutedEventArgs e)
        {
            Username = txtBedrijf.Text;
            Paswoord = txtPassword.Password;
            PaswoordBev = txtConfirmPw.Password;
            Vraag = txtVraag.Text;
            Antwoord = txtAntwoord.Password;
            Straat = txtStraat.Text;
            Postcode = txtPostcode.Text;
            Plaats = txtPlaats.Text;
            Land = txtLand.Text;
            Email = txtEmail.Text;
            Telefoon = txtTelefoon.Text;
            Medewerkers = Convert.ToInt32(txtMedewerkers.Text);
            Rol = cmbRoles.SelectedItem.ToString();
            MembershipCreateStatus result;
            if (Paswoord.Equals(PaswoordBev))
            {
                if (!(Membership.FindUsersByName(Username) == null))
                {
                    Membership.CreateUser(Username, Paswoord, Email, Vraag, Antwoord, true, out result);

                    ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
                    Company company = new Company();
                    company.Name = txtBedrijf.Text;
                    company.Street = txtStraat.Text;
                    company.ZipCode = Convert.ToInt32(txtPostcode.Text);
                    company.City = txtPlaats.Text;
                    company.Country = txtLand.Text;
                    company.Email = txtEmail.Text;
                    company.Phone = txtTelefoon.Text;
                    company.Employees = Convert.ToInt32(txtMedewerkers.Text);
                    myClient.Insert_Company(company);
                    ClearAll();

                    MessageBox.Show(result.ToString());
                }
                if (!Roles.RoleExists(Rol)){
                
                    Roles.CreateRole(Rol);}
                if (!Roles.IsUserInRole(Username, Rol)){
                    Roles.AddUserToRole(Username, Rol);}
            }
             else{
                 MessageBox.Show("De paswoorden komen niet overeen.");
                 ClearAll();
        }
            lstBedrijven.Items.Refresh();
    }
        public void ClearAll()
        {
            txtBedrijf.Text = "";
            txtPassword.Password = "";
            txtConfirmPw.Password = "";
            txtVraag.Text = "";
            txtAntwoord.Password = "";
            txtStraat.Text = "";
            txtPostcode.Text = "";
            txtPlaats.Text = "";
            txtLand.Text = "";
            txtEmail.Text = "";
            txtTelefoon.Text = "";
            txtMedewerkers.Text = "";
            cmbRoles.SelectedIndex = -1;
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Company company = new Company();
            company.Name = lstBedrijven.SelectedItem.ToString();
            company.Street = txtEditStraat.Text;
            company.ZipCode = Convert.ToInt32(txtEditPostcode.Text);
            company.City = txtEditPlaats.Text;
            company.Country = txtEditLand.Text;
            company.Email = txtEditEmail.Text;
            company.Phone = txtEditTelefoon.Text;
            company.Employees = Convert.ToInt32(txtEditMedewerkers.Text);
            myClient.Modify_Company(company);

            MessageBox.Show("Bedrijf aangepast!");




        }

        private void lstBedrijven_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            string bedrijf = lstBedrijven.SelectedItem.ToString();
            Company company = new Company();
            company = myClient.Get_CompanyByName(bedrijf);
            txtEditStraat.Text = company.Street;
            txtEditPostcode.Text = company.ZipCode.ToString();
            txtEditPlaats.Text = company.City;
            txtEditLand.Text = company.Country;
            txtEditEmail.Text = company.Email;
            txtEditTelefoon.Text = company.Phone;
            txtEditMedewerkers.Text = company.Employees.ToString();
        }

        private void btnVerwijder_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            Company company = new Company();
            company.Name = lstBedrijven.SelectedItem.ToString();

            int x = myClient.DeleteCompany(company);
            if (x > 0)
            {
                MessageBox.Show("Bedrijf verwijdert!");
            }
            else
            {
                MessageBox.Show("Bedrijf heeft nog contracten openstaan");
            }
            
            

        }
        }
    }

